//
//  Color+Ext.swift
//  VIPCommon
//
//  Created by Toj on 1/14/21.
//

import Foundation
import CoreGraphics

#if canImport(UIKit)
@_exported import UIKit
#endif

#if canImport(Cocoa)
import Cocoa
#endif

public extension TTColor {
    
    convenience init(
        red: Int,
        green: Int,
        blue: Int,
        alpha: CGFloat = 1.0
    ) {
        let red = CGFloat(red) / 255.0
        let green = CGFloat(green) / 255.0
        let blue = CGFloat(blue) / 255.0
        self.init(red: red, green: green, blue: blue, alpha: alpha)
    }
    
    convenience init(hex: Int, alpha: CGFloat = 1.0) {
        let red = CGFloat((hex & 0xFF0000) >> 16) / 255.0
        let green = CGFloat((hex & 0xFF00) >> 8) / 255.0
        let blue = CGFloat((hex & 0xFF)) / 255.0
        self.init(red: red, green: green, blue: blue, alpha: alpha)
    }

    // Hex String -> UIColor
    convenience init(hex: String, alpha: CGFloat = 1.0) {
        let hexColor = hex.trimmingCharacters(in: .whitespacesAndNewlines)
        let scanner = Scanner(string: hexColor)

        if hexColor.hasPrefix("#") {
            scanner.scanLocation = 1
        }

        var color: UInt32 = 0
        scanner.scanHexInt32(&color)

        let mask = 0x000000FF
        let r = Int(color >> 16) & mask
        let g = Int(color >> 8) & mask
        let b = Int(color) & mask

        let red   = CGFloat(r) / 255.0
        let green = CGFloat(g) / 255.0
        let blue  = CGFloat(b) / 255.0

        self.init(red: red, green: green, blue: blue, alpha: alpha)
    }
}

public extension TTColor {

    static var random: TTColor {
        // drand48()
        let red   = CGFloat.random(in: 0...1)
        let green = CGFloat.random(in: 0...1)
        let blue  = CGFloat.random(in: 0...1)

        return TTColor(red: red, green: green, blue: blue, alpha: 1)
    }
    
    func st_alpha(_ alpha: CGFloat) -> TTColor {
        tt_alpha(alpha)
    }
    
    func tt_alpha(_ alpha: CGFloat) -> TTColor {
        return withAlphaComponent(alpha)
    }
    
    // UIColor -> Hex String
    var tt_hexColor: String? {
        var red: CGFloat = 0
        var green: CGFloat = 0
        var blue: CGFloat = 0
        var alpha: CGFloat = 0

        let multiplier = CGFloat(255.0)
        getRed(&red, green: &green, blue: &blue, alpha: &alpha)

        if alpha == 1.0 {
            return String(
                format: "#%02lX%02lX%02lX",
                Int(red * multiplier),
                Int(green * multiplier),
                Int(blue * multiplier)
            )
        }
        return String(
            format: "#%02lX%02lX%02lX%02lX",
            Int(red * multiplier),
            Int(green * multiplier),
            Int(blue * multiplier),
            Int(alpha * multiplier)
        )
    }
}

#if canImport(UIKit)
public extension TTColor {
    
    var to_image: UIImage? {
        return to_image(size: UIScreen.main.bounds.size)
    }

    func to_image(size: CGSize) -> UIImage? {
        let image = UIImage.st_image(color: self, size: size)
        return image
    }
}

#endif
